package hu.bme.gabor.onlab.c2dm;
import hu.bme.gabor.onlab.MainActivity;
import hu.bme.gabor.onlab.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.provider.Settings.Secure;
import android.util.Log;
  
public class Receiver extends BroadcastReceiver {  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        String action = intent.getAction();  
          
        if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) { 
        	Log.w("C2DM", "Registration Receiver called");
            Log.w("C2DM", "Received registration ID");  
            final String registrationId = intent.getStringExtra("registration_id");  
            String error = intent.getStringExtra("error");  
  
            Log.d("C2DM", "dmControl: registrationId = " + registrationId + ", error = " + error);  
            String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);  
            createNotification(context, registrationId);  
            //sendRegistrationIdToServer(deviceId, registrationId);  
            // Also save it in the preference to be able to show it later  
            saveRegistrationId(context, registrationId);  
        }  
        
		if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
			Log.w("C2DM", "Message Receiver called");
			Log.w("C2DM", "Received message");
			final String payload = intent.getStringExtra("payload");
			Log.d("C2DM", "dmControl: payload = " + payload);
			// Note: Send this to my application server to get the real data
			// Lets make something visible to show that we received the message
			createNotification2(context, payload);

		}
    }  
  
    private void saveRegistrationId(Context context, String registrationId) {  
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  
        Editor edit = prefs.edit();  
        edit.putString("AUTH", registrationId);  
        edit.commit();  
    }  
  
    public void createNotification(Context context, String registrationId) {  
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
        Notification notification = new Notification(R.drawable.ic_launcher, "Registration successful", System.currentTimeMillis());  
        // Hide the notification after its selected  
        notification.flags |= Notification.FLAG_AUTO_CANCEL;  
  
        Intent intent = new Intent(context, Result.class);  
        intent.putExtra("registration_id", registrationId);  
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);  
        notification.setLatestEventInfo(context, "Registration", "Successfully registered", pendingIntent);  
        notificationManager.notify(0, notification);  
    }  
  
    // Incorrect usage as the receiver may be canceled at any time  
    // do this in an service and in an own thread  
    public void sendRegistrationIdToServer(String deviceId, String registrationId) {  
        Log.d("C2DM", "Sending registration ID to my application server");  
        HttpClient client = new DefaultHttpClient();  
        HttpPost post = new HttpPost("http://vogellac2dm.appspot.com/register");  
        try {  
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);  
            // Get the deviceID  
            nameValuePairs.add(new BasicNameValuePair("deviceid", deviceId));  
            nameValuePairs.add(new BasicNameValuePair("registrationid", registrationId));  
  
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
            HttpResponse response = client.execute(post);  
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));  
  
            String line = "";  
            while ((line = rd.readLine()) != null) {  
                Log.e("HttpResponse", line);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
    
	public void createNotification2(Context context, String payload) {
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.ic_launcher, "Message received", System.currentTimeMillis());
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		Intent intent = new Intent(context, MainActivity.class);
		intent.putExtra("payload", payload);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		notification.setLatestEventInfo(context, "Message", payload, pendingIntent);
		notificationManager.notify(0, notification);
	}
}  